package Demo01;

public class MyLinkedList implements Ilist{

    static class ListNode {
        public ListNode next;
        public ListNode prev;
        public int val;
        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;
    @Override
    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        if(head!=null) {
            head.prev = newNode;
            newNode.next = head;
            head = newNode;
        } else {
            head = last = newNode;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        if(head==null) {
            head = last = newNode;
        } else {
            last.next = newNode;
            newNode.prev = last;
            last = newNode;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        if(index<0||index>size()) {
            System.out.println("index  违法");
            return;
        }
        if(index==0) {
            addFirst(data);
            return;
        }
        if(index==size()) {
            addLast(data);
            return;
        }
        int count = 0;
        ListNode curNode = head;
        ListNode node = new ListNode(data);
        while(count!=index) {
            curNode = curNode.next;
            count++;
        }
        node.prev = curNode.prev;
        node.next = curNode;
        curNode.prev.next = node;
        node.next.prev = node;

    }

    @Override
    public boolean contains(int key) {
        ListNode curNode = head;
        while(curNode!=null) {
            if(curNode.val==key) {
                return true;
            }
            curNode = curNode.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head.val==key) {
            head.next.prev = null;
            head = head.next;
            return;
        }
        ListNode curNode = head;
        while (curNode!=null) {
            if(curNode.val==key) {
                break;
            }
            curNode = curNode.next;
        }
        if(curNode==null) {
            System.out.println("没有该值,无法删除");
            return;
        }
        curNode.prev.next = curNode.next;
        if(curNode!=last) {
            curNode.prev = curNode.prev;
        } else {
            last = last.prev;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head==null) {
            return;
        }
        while(head!=null&&head.val==key) {
            head = head.next;
        }
        if(head==null) {
            last = null;
            return;
        } else {
            head.prev = null;
        }
        ListNode cur = head;
        while (cur!=null) {
            if(cur.val == key) {
                cur.prev.next = cur.next;
                if(cur!=last) {
                    cur.next.prev = cur.prev;
                } else {
                    last = last.prev;
                }

            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode curNode = head;
        while(curNode!=null) {
            count++;
            curNode = curNode.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while (cur!=null) {
            ListNode tmp = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = tmp;
        }
        head = null;
        last = null;
    }

    @Override
    public void display() {
        ListNode curNode = head;
        while(curNode!=null) {
            System.out.print(curNode.val+" ");
            curNode = curNode.next;
        }
        System.out.println();
    }
}
